﻿


function ValidData() {

    var txtUserCount = document.getElementById(mstrControlProfix + "txtUserCount");
    var txtPassword = document.getElementById(mstrControlProfix + "txtPassword");
    var txtUserName = document.getElementById(mstrControlProfix + "txtUserName");
    var txtConfirmPassword = document.getElementById(mstrControlProfix + "txtConfirmPassword");
    var txtUserName = document.getElementById(mstrControlProfix + "txtUserName");
    var txtEmail = document.getElementById(mstrControlProfix + "txtEmail");


    if (txtUserCount.value == "") {
        alert("用户帐号不能为空。");
        txtUserCount.focus();
        return false;
    }
    if (txtPassword.value == "") {
        alert("密码不能为空。");
        txtPassword.focus();
        return false;
    }
    if (txtConfirmPassword.value == "") {
        alert("确认密码不能为空。");
        txtConfirmPassword.focus();
        return false;
    }
    if (txtPassword.value !=txtConfirmPassword.value) {
        alert("密码与确认密码不一致。");
        txtConfirmPassword.focus();
        return false;
    }
    if (txtUserName.value == "") {
        alert("用户姓名不能为空。");
        txtUserName.focus();
        return false;
    }
    if (txtEmail.value == "") {
        alert("邮箱不能为空。");
        txtEmail.focus();
        return false;
    }

    if (!(/[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/.test(txtEmail.value)) && txtEmail.value != "") {
        alert("请输入正确的邮箱格式。例如：<XXXX@XXX.XXX>。");
        txtEmail.focus();
        return false;
    }

    return true;
}





var PasswordStrength = {
    Level: ["height", "normal", "weak"],
    LevelValue: [30, 20, 0],
    Factor: [1, 2, 5],
    KindFactor: [0, 0, 10, 20],
    Regex: [/[a-zA-Z]/g, /\d/g, /[^a-zA-Z0-9]/g]
}
PasswordStrength.StrengthValue = function(pwd) {
    var strengthValue = 0;
    var ComposedKind = 0;
    for (var i = 0; i < this.Regex.length; i++) {
        var chars = pwd.match(this.Regex[i]);
        if (chars != null) {
            strengthValue += chars.length * this.Factor[i];
            ComposedKind++;
        }
    }
    strengthValue += this.KindFactor[ComposedKind];
    return strengthValue;
}
PasswordStrength.StrengthLevel = function(pwd) {
    var value = this.StrengthValue(pwd);
    for (var i = 0; i < this.LevelValue.length; i++) {
        if (value >= this.LevelValue[i])
            return this.Level[i];
    }
}

function CreateRatePasswdReq(passwd) {
    var min_passwd_len = 8;

    if (passwd.length >= min_passwd_len) {
        var StrengthValue = PasswordStrength.StrengthValue(passwd);

        if (StrengthValue <= 20) {
            DrawBar(1);

        }
        else if (StrengthValue <= 30) {
            DrawBar(2);
        }
        else if (StrengthValue > 30) {
            DrawBar(3);
        }
    } else {
        DrawBar(0);
        if (passwd.length == 0) {
            document.getElementById('lblStrengthSituation').innerHTML = "";
        }
    }
}

function DrawBar(rating) {
    ratingMsgs = new Array(4);
    ratingMsgColors = new Array(4);
    barColors = new Array(4);
    barWidth = new Array(4);

    ratingMsgs[0] = "太短";
    ratingMsgs[1] = "弱";
    ratingMsgs[2] = "中等";
    ratingMsgs[3] = "强";

    ratingMsgColors[0] = "#676767";
    ratingMsgColors[1] = "#aa0033";
    ratingMsgColors[2] = "#f5ac00";
    ratingMsgColors[3] = "#008000";

    barColors[0] = "#dddddd";
    barColors[1] = "#aa0033";
    barColors[2] = "#ffcc33";
    barColors[3] = "#008000";

    barWidth[0] = "0";
    barWidth[1] = "33%";
    barWidth[2] = "66%";
    barWidth[3] = "100%";


    var posbar = document.getElementById('StuBar');
    var lblSituation = document.getElementById('lblStrengthSituation');

    lblSituation.innerHTML = ratingMsgs[rating];
    lblSituation.style.color = ratingMsgColors[rating];
    posbar.style.backgroundColor = barColors[rating];
    posbar.style.width = barWidth[rating];
}